#coding=utf-8
'''
Created on 9.20, 2016
@author: wangpeng
'''
import unittest
from appium import webdriver
from selenium.webdriver.common.by import By   #用于匹配某个值是否出现，定时等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import common.config
import common.logging_save_huawei_5x
import common.desired_setting_huawei_5x
import threading
import re
import common.Create_Log_Folder
import os
import os.path
import common.logging_save_huawei_5x
import serial
#import common.save_serial_log
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import subprocess

class Test(unittest.TestCase):
    def setUp(self):
        os.system('taskkill /f /im adb.exe')
        common.logging_save_huawei_5x.logging.info("app测试开始，正在开始启动app")
        print time.ctime(),"app测试开始，正在开始启动app"
        self.begin_time_for_serial = time.strftime("%y%m%d_%H:%M:%S", time.localtime()) 
        self.save_log = common.Create_Log_Folder.Create("\\huawei_5x\\just_wait_connect_speical")    #建立report保存路径
        common.logging_save_huawei_5x.logging.info("app连接，不做任何操作，观察是否会发生断开现象")
        print time.ctime(),"进去到设备管理连接界面"
        sys.path.append('\adbcommand')  #增加adb log
        self.test = subprocess.Popen(r'E:\eclipse\huawei_rader\all_mobile\testcase\huawei_5x\adbcommond\just_wait_connect_speical.bat') #导入adb
        time.sleep(1)
        
        #开启appium设置
        self.desired_setting = common.desired_setting_huawei_5x.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)
        common.logging_save_huawei_5x.logging.info("app连接BLE中，70秒后再做判定。。。")
        print time.ctime(),"app连接BLE中，70秒再做判定。。。"
        time.sleep(70)
        try:
            self.driver.find_element_by_id("com.huawei.smallRadar:id/sure").click()
            time.sleep(3)
            common.logging_save_huawei_5x.logging.info("close弹出框")
            print time.ctime(),"close弹出框"

        except:
            pass

    def tearDown(self):
        self.driver.quit()
        time.sleep(1)
        self.test.kill()
        os.system('taskkill /f /im adb.exe')
        print '关闭adb log'
        time.sleep(10)
        print 'finished'
        
    def test_play(self):
        u"""测试步骤：无任何操作，观察2个小时，蓝牙是否会断开。如果发生蓝牙断开或者死机现象，则宝宝。"""

        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        #判断是否进入主界面,没有断开提醒
        testtime = 1
        common.logging_save_huawei_5x.logging.info("未做任何操作，只是等待，观察小雷达是否会发生断开现象")
        print time.ctime(),"未做任何操作，只是等待，观察小雷达是否会发生断开现象"
        def valiate_ok():
            try:
                WebDriverWait(self.driver, 14400).until(EC.presence_of_element_located((By.ID,common.config.BLE_disconnect())))
                common.logging_save_huawei_5x.logging.info("发现蓝牙断开现象")
                print time.ctime(),"发现蓝牙断开现象"
                a = 1
                self.driver.get_screenshot_as_file(save_pic)
                return a
                                            
            except:
                common.logging_save_huawei_5x.logging.info("经过确认，蓝牙正常连接")
                print time.ctime(),"经过确认，蓝牙正常连接"
                a= 0
                return a
                
        self.assertNotEqual(valiate_ok(), 1, "发现连接断开现象，请确认问题所在")
        common.logging_save_huawei_5x.logging.info("经过确认，蓝牙正常连接成功")
        print time.ctime(),"经过确认，蓝牙正常连接成功"

if __name__ == "__main__":
    unittest.main()